<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination

try{
	$verifyToken = md5('unique_salt' . $_POST['timestamp']);
	if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
		$redata = array();
		require('../Common.php');
		$tempFile = $_FILES['Filedata']['tmp_name'];
		$folder = $_POST['folder']
		$targetPath = $_SERVER['DOCUMENT_ROOT'] . $folder . "/uploads/stream/".date("Y")."/".date("m");
		if(!file_exists($targetPath))
			Common::rmkdir($targetPath);
		$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
		
		// Validate the file type
		$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
		$fileParts = pathinfo($_FILES['Filedata']['name']);
		
		if (!in_array($fileParts['extension'],$fileTypes))
			throw new Exception('Invalid file type');
		move_uploaded_file($tempFile,$targetFile);
		$redata["realpath"] = $targetFile;
		$redata["path"] = str_replace("", "", $targetFile);
		echo json_encode(array("data" => $redata, "msg" => "success", "ret" => 200));
		exit();
	}else
		throw new Exception("config error");
}catch(Exception $e){
	echo json_encode(array("data" => "", "msg" => $e->getMessage(), "ret" => 400));
    exit();
}

?>